<template>
  <view class="singleMain">
    <view class="singleBox">
      <view class="terminalzcModifyItem">
        <view class="title">输入机具号</view>
        <view class="inputBox">
          <u-form-item label="机具编号" label-width="150rpx">
            <u-input
              placeholder="请输入机具编号"
              v-model="inputValue"
              style="font-weight: bold"
              @input="inputChange"
            />
          </u-form-item>
		  <!-- <u-icon class="cuIcon-scan size" @click="scanTab"></u-icon> -->
        </view>
        <view class="inputItem">
          <u-form-item label="所属政策" label-width="150rpx">
            <u-input
              @tap="getProductListBySn"
              :disabled="true"
              :value="currentProduct.label"
              placeholder="点击获取当前机具所属政策"
            />
            <slot>
              <view class="slotRight" @tap="selectProduct">修改</view>
            </slot>
          </u-form-item>
        </view>
        <u-button
          :ripple="true"
          @click="nextStep()"
          class="rButton"
          type="primary"
          >下一步</u-button
        >
      </view>
    </view>
    <u-select
      v-model="show"
      confirm-color="#fb4535"
      :list="productListNew"
      @confirm="productConfirm"
    ></u-select>
    <u-toast ref="uToast" />
  </view>
</template>
<script>
export default {
  data() {
    return {
      show: false,
      inputValue: "",
      currentProduct: "",
      currentProduct: {
        label: "",
        value: "",
      },
      productList: [],
      productListNew: [],
    };
  },
  methods: {
    nextStep() {
      let $inputValue = this.$u.trim(this.inputValue, "all");
      if ($inputValue == "") {
        this.$refs.uToast.show({
          title: "请输入机具号",
          type: "error",
        });
        return;
      }
      if (this.currentProduct.value == "") {
        this.$refs.uToast.show({
          title: "请获取当前机具所属政策",
          type: "error",
        });
        return;
      }
      this.$u.route({
        url: "/pages/zdzc/zdzc2",
        type: "redirect",
        params: {
          type: "single",
          singleInputValue: this.inputValue,
          productValue: this.currentProduct.value,
        },
      });
    },
    //根据SN号获取产品列表
    getProductListBySn() {
      let $inputValue = this.$u.trim(this.inputValue, "all");
      if ($inputValue != "") {
        this.$u.api
          .getProductListBySn({
            beginSn: $inputValue,
            endSn: $inputValue,
          })
          .then((res) => {
            if (res.status == 200) {
              let list = res.result.list;
              let curIndex = res.result.curIndex;
              this.productList = list;
              if (curIndex >= 0) {
                this.currentProduct = list[curIndex];
              }
              this.productListNew = this.productList.filter(item => item.hideFlag!='1')
            } else {
              this.$refs.uToast.show({
                title: res.msg,
                type: "error",
              });
              this.inputValue = "";
              this.productList = [];
              this. currentProduct={label: "",value: ""}
            }
          })
          .catch((res) => {
            if (res.errMsg == "request:fail timeout") {
              this.$u.toast("请求超时，请重试!");
            } else if (res.errMsg == "request:fail") {
              this.$u.toast("请求失败，请重试");
            } else {
               this.$u.toast("请求失败，请重试");
            }
            this.$loading(false);
          });
      } else {
        this.$refs.uToast.show({
          title: "请输入机具号",
          type: "error",
        });
      }
    },
    selectProduct() {
      let inputValue = this.$u.trim(this.inputValue, "all");
      if (inputValue == "") {
        this.$refs.uToast.show({
          title: "请输入机具号",
          type: "error",
        });
        return;
      }
      if (this.productList.length == 0) {
        this.$refs.uToast.show({
          title: "请先获取机具政策",
          type: "error",
        });
        return;
      }
      if (this.currentProduct.value == "") {
        this.$refs.uToast.show({
          title: "请获取当前机具所属政策",
          type: "error",
        });
        return;
      }
      this.show = true;
    },
    productConfirm(e) {
      this.currentProduct = e[0];
    },
    inputChange() {
      this.productList = [];
      this.currentProduct = { label: "", value: "" };
    },
	
	
	// 扫码识别
	scanTab(){
		uni.scanCode({
		  scanType: ["barCode"],
		  success: function (res) {
		    let tNum = res.result;
			this.inputValue = tNum
		  },
		  fail: function (res) {
		  	this.$refs.uToast.show({
		  	  title: "识别失败，请手动输入",
		  	  type: "error",
		  	});
			this.inputValue = ''
		  }
		});
	}
	
  },
};
</script>
<style lang="scss">
page {
  background: #fff;
}
.singleMain {
  padding-top: 20rpx;
  .terminalzcModifyItem {
    .title {
      font-size: 36rpx;
      font-weight: bold;
      padding-bottom: 20rpx;
    }
    padding: 50rpx 60rpx;
    .inputBox {
		display: flex;
		justify-content: space-between;
		
		.size{
			font-size: 44rpx;
		}
    }
    .rButton {
      margin-top: 60rpx;
      background: #fb4535;
      height: 70rpx;
      line-height: 70rpx;
    }
  }
}
.slotRight {
  padding-left: 30rpx;
  color: #fb4535;
  font-weight: bold;
}
</style>